import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:hrf_app/common/values/svgs.dart';
import 'package:hrf_app/common/widgets/text_input_edit.dart';
import 'package:hrf_app/common/widgets/text_input_image_edit.dart';
import 'package:hrf_app/common/widgets/text_input_multiple_edit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'index.dart';

class ProjectBugDetailPage extends GetView<ProjectBugDetailPageController> {
  const ProjectBugDetailPage({super.key});

  // 主视图
  Widget _buildView() {
    int? priority = controller.projectBugModel.priority;
    int? status = controller.projectBugModel.status;
    String priorityName = "低";
    TDTagTheme tagThemePriority = TDTagTheme.primary;
    if (priority == 2) {
      priorityName = "中";
      tagThemePriority = TDTagTheme.warning;
    }
    if (priority == 3) {
      priorityName = "高";
      tagThemePriority = TDTagTheme.danger;
    }
    String statusName = "未完成";
    TDTagTheme tagThemeStatus = TDTagTheme.danger;
    if (status == 5) {
      statusName = "已暂停";
      tagThemeStatus = TDTagTheme.warning;
    }
    if (status == 10) {
      statusName = "已解决";
      tagThemeStatus = TDTagTheme.primary;
    }
    if (status == 20) {
      statusName = "已关闭";
      tagThemeStatus = TDTagTheme.success;
    }
    return ListView(
      children: [
        TextInputEdit(
          labelText: "ID:",
          text: controller.projectBugModel.idSort.toString(),
        ),
        TextInputEdit(
          labelText: "所属项目:",
          text: controller.projectBugModel.projectName,
        ),
        TDDivider(),
        TextInputMultipleEdit(
          labelText: "标题:",
          text: controller.projectBugModel.title,
          isShowEdit: controller.projectBugModel.status == 0 ? true : false,
          isCustomEdit: true,
          editWidget: Icon(
            TDIcons.edit_1,
            color: Colors.blue,
          ),
          isMustBeEnter: true,
          editCallBack: (val) {
            controller.editDialog(
                controller.projectBugModel.title ?? "", "title", "标题");
          },
        ),
        TDDivider(),
        TextInputMultipleEdit(
            labelText: "内容:",
            text: controller.projectBugModel.content,
            isShowEdit: controller.projectBugModel.status == 0 ? true : false,
            isCustomEdit: true,
            editWidget: Icon(
              TDIcons.edit_1,
              color: Colors.blue,
            ),
            editCallBack: (val) {
              controller.editDialog(
                  controller.projectBugModel.content ?? "", "content", "内容");
            }),
        TDDivider(),
        TextInputEdit(
          labelText: "指派人:",
          text: controller.projectBugModel.assignUserName,
          isShowEdit: controller.projectBugModel.status == 0 ? true : false,
          isCustomEdit: true,
          editWidget: SvgPicture.asset(
            AssetsSvgs.pointRight, // 使用 SVG 图标
            width: 18,
            height: 18,
            color: Colors.blueAccent,
          ),
          isMustBeEnter: true,
          editCallBack: (val) {
            controller.editAssignUserDialog(
                controller.projectBugModel.assignUserName ?? "",
                "assignUserName",
                "指派人");
          },
        ),
        TDDivider(),
        TextInputEdit(
          labelText: "状态:",
          isCustomText: true,
          textWidget: TDTag(statusName, theme: tagThemeStatus),
        ),
        TDDivider(),
        TextInputEdit(
          labelText: "优先级:",
          isCustomText: true,
          textWidget:
              TDTag(priorityName, isOutline: true, theme: tagThemePriority),
        ),
        TDDivider().toVisibility(visible: status == 10),
        TextInputEdit(
          labelText: "解决时间:",
          text: controller.projectBugModel.solveTime,
        ).toVisibility(visible: status == 10),
        TDDivider(),
        TextInputEdit(
          labelText: "暂停原因:",
          text: controller.projectBugModel.suspendReason,
        ),
        TDDivider(),
        TextInputEdit(
          labelText: "创建人:",
          text: controller.projectBugModel.creatorName,
        ),
        TDDivider(),
        TextInputEdit(
          labelText: "创建时间:",
          text: controller.projectBugModel.createTime,
        ),
        TDDivider(),
        TextInputImageEdit(
          labelText: "附件:",
          isShowAdd: controller.projectBugModel.status == 0 ? true : false,
          uploadFileModelList: controller.uploadFileModelList,
          delCallBack: (index) {
            controller.delImage(index);
          },
          isShowDel: controller.projectBugModel.status == 0 ? true : false,
          addCallBack: (dynamic param) {
            // 弹出底部弹窗选择相机或相册
            Get.bottomSheet(
              Container(
                height: 150,
                alignment: Alignment.center,
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ListTile(
                      leading: const Icon(Icons.camera),
                      title: const Text("相机"),
                      onTap: () {
                        controller.selectPhoto(ImageSource.camera);
                        Get.back(); // 关闭底部弹窗
                      },
                    ),
                    ListTile(
                      leading: const Icon(Icons.photo_library),
                      title: const Text("相册"),
                      onTap: () {
                        controller.selectMultiImage();
                        Get.back(); // 关闭底部弹窗
                      },
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProjectBugDetailPageController>(
      init: ProjectBugDetailPageController(),
      id: "projectBugDetailPage",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("BUG详情"),
          ),
          body: SafeArea(
            child: Column(
              children: [
                Expanded(child: _buildView()),
                BottomAppBar(
                  color: Colors.white,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Expanded(
                          child: TDButton(
                        text: '暂停',
                        margin: EdgeInsets.symmetric(horizontal: 5),
                        size: TDButtonSize.large,
                        type: TDButtonType.outline,
                        shape: TDButtonShape.rectangle,
                        onTap: controller.suspendProjectBug,
                      )).toVisibility(
                          visible: controller.projectBugModel.status == 0),
                      Expanded(
                          child: TDButton(
                        text: '重启',
                        margin: EdgeInsets.symmetric(horizontal: 5),
                        size: TDButtonSize.large,
                        type: TDButtonType.fill,
                        shape: TDButtonShape.rectangle,
                        theme: TDButtonTheme.primary,
                        onTap: controller.restartProjectBug,
                      )).toVisibility(
                          visible: controller.projectBugModel.status == 5),
                      Expanded(
                          child: TDButton(
                        text: '解决',
                        margin: EdgeInsets.symmetric(horizontal: 5),
                        size: TDButtonSize.large,
                        type: TDButtonType.fill,
                        shape: TDButtonShape.rectangle,
                        theme: TDButtonTheme.primary,
                        onTap: controller.solveProjectBug,
                      )).toVisibility(
                          visible: controller.projectBugModel.status == 0),
                      Expanded(
                          child: TDButton(
                        text: '激活',
                        margin: EdgeInsets.symmetric(horizontal: 5),
                        size: TDButtonSize.large,
                        type: TDButtonType.fill,
                        shape: TDButtonShape.rectangle,
                        theme: controller.projectBugModel.status == 20
                            ? TDButtonTheme.primary
                            : null,
                        onTap: controller.activeProjectBug,
                      )).toVisibility(
                          visible: controller.projectBugModel.status == 20 ||
                              controller.projectBugModel.status == 10),
                      Expanded(
                          child: TDButton(
                        text: '关闭',
                        margin: EdgeInsets.symmetric(horizontal: 5),
                        size: TDButtonSize.large,
                        type: TDButtonType.fill,
                        shape: TDButtonShape.rectangle,
                        theme: TDButtonTheme.primary,
                        onTap: controller.closeProjectBug,
                      )).toVisibility(
                          visible: controller.projectBugModel.status == 10),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
